/*
 * @Author: ljc
 * @Date: 2025-02-12 09:15:36
 * @LastEditors: g05047
 * @LastEditTime: 2025-02-14 16:21:05
 * @Description: 参数弹框方法
 */
import XEUtils from 'xe-utils';

export const useParamHandle = ({ props, tableRef, codeEditorStore, codeDialogRef }) => {
    const tableColumns = reactive([
        { field: 'field', title: '参数字段', align: 'left', width: 200, editRender: { autofocus: '.el-input__inner' }, slots: { default: 'field_input', edit: 'field_input' } },
        { field: 'kind', title: '参数类型', align: 'left', width: 150, editRender: { autofocus: '.el-input__inner' }, slots: { default: 'kind_select', edit: 'kind_select' }, },
        { field: 'defaultValue', title: '默认值', width: 150, showOverflow: true, align: 'center', editRender: { autofocus: '.el-input__inner' }, slots: { default: 'default_input', edit: 'default_input' } },
        { field: 'paramRender', title: '参数编辑', fixed: 'right', width: 100, showOverflow: true, align: 'center', slots: { default: 'param_render', } },
    ])
    const tableData = computed(() => {
        const paramColumns = XEUtils.clone(props.dataSource.sqlConfig?.paramColumns || [], true)
        return paramColumns
    })
    /**
     * 添加新的行数据
     */
    const handleAddRow = () => {
        const gridRef = tableRef.value;
        const rowItem = {
            "field": "",
            "kind": 0,
            "defaultValue": "",
            "paramRender": ""
        }
        gridRef.insertAt(rowItem, -1)
    }
    /** 删除行 */
    const handleDelRow = () => {
        const gridRef = tableRef.value;
        gridRef.removeCheckboxRow()
    }
    /** 获取表格数据 */
    const handleGetParamTable = () => {
        const gridRef = tableRef.value;
        const { tableData } = gridRef.getTableData()
        return tableData
    }
    /**
     * 打开编辑面板
     */
    const codeEditorOpen = ({ row, column }) => {
        codeEditorStore.row = row
        codeEditorStore.column = column
        codeDialogRef.value.handleOpen(row.paramRender)
    }
    /**
     *编辑面板回调
     */
    const handleConfirm = (content) => {
        const { row, column } = codeEditorStore
        row[column.field] = content
    }
    return {
        tableData,
        tableColumns,
        handleAddRow,
        handleDelRow,
        handleGetParamTable,
        codeEditorOpen,
        handleConfirm
    }
}